import { Command } from '../constant';
import { IMainReducer } from '../types';
import { Action } from 'typings';
import produce from 'immer';
import * as immerUtil from '@/redux/immer-util';

const INITIAL_STATE: IMainReducer = {
  isReady: false,
  isLoading: false,
  payModel: false,
  cancelModel: false,
  confirmModel: false,
  orderType: 1,
  orderList: [],
  pageNum: 0,
  pageSize: 10,
  totalPages: 0,
  escortTradeStatus: null, //我的发布订单状态
  escortSnatchOrderStatus: null, //我的接单订单状态
  escortTradeId: '', //订单id
  harvestCode: '', //收货编码
  cancellationOptions: 0, //取消类型
  cancellationReason: '', //取消原因
  price: 0, //订单金额
  accountBalanceTotal: 0, //可用余额
  ReleasedTab: [//我的发布tab
    { label: '全部', key: null, num: 0 },
    // { label: '待支付', key: '0', num: 0 },
    // {label: '待接单', key: '1', num: 0},
    // {label: '服务中', key: '2', num: 0},
    // {label: '已完成', key: '3', num: 0},
    // {label: '已取消', key: '4', num: 0},
    { label: '待接单', key: '0', num: 0 },
    { label: '服务中', key: '1', num: 0 },
    { label: '已完成', key: '2', num: 0 },
    { label: '已取消', key: '3', num: 0 },
  ],
  ReceivingTab: [//我的接单tab
    { label: '全部', key: null, num: 0 },
    { label: '服务中', key: '0', num: 0 },
    { label: '已完成', key: '1', num: 0 },
    { label: '已取消', key: '2', num: 0 },
  ],
  points: {
    //用户拥有的总积分
    totalPoint: 0,
    //最多可使用的积分
    maxPoint: 0,
    //用戶使用的积分
    usePoint: 0,
    pointConfig: {},
    // 积分抵扣金额
    deductionAmountOfPoints: 0,
    // 积分抵扣开关
    integralDeductionSwitch: false,
  },
};

export default function main(state = INITIAL_STATE, action: Action): IMainReducer {
  const { type, payload } = action;

  return produce<IMainReducer>(state, (draftState) => {
    switch (type) {
      //通用改变数据
      case Command.commonChange:
        return immerUtil.commonChange(draftState, { ...payload, key: 'main' });

      //初始化
      case Command.init:
        draftState.isReady = true;
        for (let propKey in payload.main) {
          draftState[propKey] = payload.main[propKey];
        }
        return draftState;

      //重置
      case Command.clean:
        for (let propKey in INITIAL_STATE) {
          draftState[propKey] = INITIAL_STATE[propKey];
        }
        return draftState;
    }
  });
}
